package component;

import com.sun.deploy.net.HttpRequest;
import constant.Constant;
import dbOperation.DbSelect;

/**
 * Created by v on 17-5-23.
 */
public class Component {
    /**
     * 根据页面标号构造分页条
     * @param currentPage 页面标号
     * @return 构造分页条的html片段
     */
    public static String pageBar(int currentPage){
        StringBuffer buffer=new StringBuffer();

        int amount= DbSelect.amount();
        int pages=amount/ Constant.PAGESIZE+1;

        int begin;
        int end;

        //计算是否需要显示缩进版的页码
        if(pages-currentPage>=Constant.MAXSIZE
                &&currentPage>Constant.MAXSIZE/2){
            begin=currentPage-Constant.MAXSIZE/2;
            end=currentPage+Constant.MAXSIZE/2;
        }else {
            begin=1;
            end=Math.min(Constant.MAXSIZE,pages)+1;
        }

        //根据计算的数据构建分页条
        for(int i=begin;i<end;i++){
            if(i==currentPage){
                buffer.append("<td><h3> "+i+" </h3></td>");
            }else {
                buffer.append("<td> <a href='/SelectServlet?page="+i+"'><h3>"+i+"</h3></a> </td>");
            }
        }

        return buffer.toString();
    }
}
